<?

/***************************************************************************
 Jupiter Content System @ Jupiterportal.com
 Copyright (C) 2005 Cosmin Flavius (highstrike@gmail.com)

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 More Info About The Licence At http://www.gnu.org/copyleft/gpl.html
****************************************************************************/

function b_poll()
{
	$name_function = "b_poll";

	global $l, $db, $blocks_db, $template, $language, $config, $misc, $PHP_SELF, $is_loged_in, $is_moderator, $is_administrator, $is_webmaster, $months, $days, $m, $y, $OnlineGuestsCount, $OnlineMembersCount;
	if(!file_exists("blocks/$name_function/language/{$config['language']}")) include "blocks/$name_function/language/en.php";
	else include "blocks/$name_function/language/{$config['language']}";

	$random_code = md5("$name_function");
	$random_code = substr("$random_code", 0, 5);

	if(!isset($content)) $content = NULL;
	if(!isset($_GET["$random_code"])) $_GET["$random_code"] = NULL;

	if($_GET["$random_code"] == "on") $_SESSION["$random_code"] = "on";
	elseif($_GET["$random_code"] == "off") $_SESSION["$random_code"] = "off";

	$query_string = "?{$_SERVER['QUERY_STRING']}";

	foreach($blocks_db as $blocks)
	{
		$block_code = md5("{$blocks['name']}");
		$block_code = substr("$block_code", 0, 5);

		$query_string = str_replace("&$block_code=off","", $query_string);
		$query_string = str_replace("&$block_code=on","", $query_string);
	}

	if($query_string == "?" || strpos($query_string, '=on') !== false || strpos($query_string, '=off') !== false) $code_query = "?";
	else $code_query = "$query_string&";

	if(!isset($_SESSION["$random_code"]) || $_SESSION["$random_code"] == "on") $links = "<a href='$code_query$random_code=off'><img src='templates/$template/images/misc/minus.gif' border='0'> {$language_block['Poll title']}</a>";
	elseif($_SESSION["$random_code"] == "off") $links = "<a href='$code_query$random_code=on'><img src='templates/$template/images/misc/plus.gif' border='0'> {$language_block['Poll title']}</a>";

	$template_block = implode("", file("templates/$template/block.php"));
	$template_block = str_replace("{template}", $template, $template_block);
	$template_block = str_replace("{links}", $links, $template_block);

	if(!isset($_SESSION["$random_code"]) || $_SESSION["$random_code"] == "on")
	{
		$poll = $db->getLine("SELECT * FROM poll ORDER BY id DESC LIMIT 1");

		$Total_poll_votes = $poll['votes1'] + $poll['votes2'] + $poll['votes3'] + $poll['votes4'] + $poll['votes5'] + $poll['votes6'] + $poll['votes7'] + $poll['votes8'] + $poll['votes9'] + $poll['votes10'];

		if($poll != FALSE)
		{
			if($l == "save_poll")
			{
				if(@$_SESSION["jcmpoll_{$poll['id']}"] == "{$poll['id']}" || @$_COOKIE["jcmpoll_{$poll['id']}"] == "{$poll['id']}"){ header("location: $PHP_SELF?l=results_poll"); exit;}
				if(!$_POST['answer']){ header("location: $PHP_SELF?&i=77"); exit;}

				$tmp["votes{$_POST['answer']}"] = $poll["votes{$_POST['answer']}"] + 1;
				$db->updateRow("poll",$tmp,"id=".$db->escape($poll['id'])."");

				$_SESSION["jcmpoll_{$poll['id']}"] = $poll['id'];
				setcookie("jcmpoll_{$poll['id']}", $poll['id'], time() + 86400);

				$query_string = "?{$_SERVER['QUERY_STRING']}";

				if($query_string == "?") $query_code = "?l=results_poll";
				else $query_code = "$query_string&l=results_poll";

				header("location: $PHP_SELF$query_code");
			}
			elseif(@$_SESSION["jcmpoll_{$poll['id']}"] == "{$poll['id']}" || @$_COOKIE["jcmpoll_{$poll['id']}"] == "{$poll['id']}" || $l == "results_poll")
			{
				$content .= "<tr><td class='con1' align='center'><b>{$poll['question']}</b></td></tr>";

				for($i = 1; $i <= 10; $i++)
				{
					if($Total_poll_votes != "0") $poll_percent = round(($poll["votes$i"] * 100 / $Total_poll_votes), 1);
					else $poll_percent = 0;

					if($poll["option$i"] != NULL) $content .= "<tr><td class='con1'><table width='100%' cellspacing='0' cellpadding='0'><tr class='empty'><td width='80%'>{$poll["option$i"]} [{$poll["votes$i"]} v.]</td><td width='20%' align='right'>$poll_percent%</td></tr><tr class='empty'><td colspan='2'><table width='100%' cellpadding='0' cellspacing='0'><tr><td width='$poll_percent%'><table width='100%' cellpadding='0' cellspacing='0'><tr><td width='2'><img src='templates/$template/images/progress/a.gif' width='2' height='16'></td><td width='100%' background='templates/$template/images/progress/b.gif'></td><td width='2'><img src='templates/$template/images/progress/c.gif' width='2' height='16'></td></tr></table></td><td width='100%'></td></tr></table></td></tr></table></td></tr>";
				}

				$content .= "<tr><td class='con1' align='center'>{$language_block['Poll votes']} $Total_poll_votes</td></tr>";
			}
			else
			{
				$query_string = "?{$_SERVER['QUERY_STRING']}";

				if($query_string == "?") $query_code = "?l=results_poll";
				else $query_code = "$query_string&l=results_poll";

				$content .= "
					<form method='post' action='$PHP_SELF?{$_SERVER['QUERY_STRING']}'>
					<tr><td class='con1' align='center'><b>{$poll['question']}</b></td></tr>";

				for($i = 1; $i <= 10; $i++)
					if($poll["option$i"] != NULL) $content .= "<tr><td class='con1'><input type='radio' name='answer' value='$i'>{$poll["option$i"]}</td></tr>";

				$content .= "
					<tr><td class='con1' align='center'><input type='submit' style='width: 100%' class='box' name='Submit' value='{$language_block['Poll submit']}'><input type='hidden' name='l' value='save_poll'></td></tr>
					<tr><td class='con1' align='center'><a href='$PHP_SELF$query_code'>{$language_block['Poll results']}</a></td></tr>";
						
				$content .= "</form>";
			}
		}
		else
		{
			$content .= "
				<tr>
				<td class='con1' align='center'>{$language_block['Poll blank']}</td>
				</tr>";
		}
	}

	$template_block = str_replace("{content}", $content, $template_block);
	$output = $template_block;

return $output;
}

?>